//
//  BaseDialog.m
//  citzenWorkStation
//
//  Created by yang qing on 2018/3/4.
//  Copyright © 2018年 yang qing. All rights reserved.
//

#import "QuitDialog.h"
@interface QuitDialog()
@property(strong,nonatomic)UIView *viewContaint;
@end

@implementation QuitDialog

- (void)show{
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self];
    [self showAlertAnimation];
//    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
//    NSArray *windowViews = [window subviews];
//    if(windowViews && [windowViews count] > 0){
//        UIView *subView = [windowViews objectAtIndex:[windowViews count]-1];
//        for(UIView *aSubView in subView.subviews)
//        {
//            [aSubView.layer removeAllAnimations];
//        }
//        [subView addSubview:self];
//        [self showAlertAnimation];
//    }
}
-(void)showAlertAnimation
{
    CAKeyframeAnimation * animation;
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 0.30;
    animation.removedOnCompletion = YES;
    animation.fillMode = kCAFillModeForwards;
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    [_viewContaint.layer addAnimation:animation forKey:nil];
}

- (void)hide{
    [self closeDialog];
}

- (instancetype)init {
    if( self = [super init] ) {
        self.frame = [[UIScreen mainScreen] bounds];
        self.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.5];
        [self addViewContain];
        [self addTips:@"请选择"];
        [self addChangeButton];
        [self addQuitButton];
        UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeDialog)];
        [self addGestureRecognizer: tap];
    }
    return self;
}


- (void)addViewContain{
     self.viewContaint  = [[UIView alloc] init];
     self.viewContaint .frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width-80, 200);
     self.viewContaint .center = [UIApplication sharedApplication].keyWindow.center;
     self.viewContaint .backgroundColor = [UIColor whiteColor];
     [self addSubview: self.viewContaint ];
    //加阴影
     self.viewContaint .layer.shadowColor = [UIColor blackColor].CGColor;//shadowColor阴影颜色
     self.viewContaint .layer.shadowOffset = CGSizeMake(6,6);//shadowOffset阴影偏移,x向右偏移4，y向下偏移4，默认(0, -3),这个跟shadowRadius配合使用
     self.viewContaint .layer.shadowOpacity = 0.6;//阴影透明度，默认0
    //添加一个空的处理事件 不让事件继续传递给上层view
    [self.viewContaint addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget:self action:nil]];
}


- (void)addTips:(NSString *)tipMsg{
    UILabel *tips = [[UILabel alloc]init];
    tips.text =tipMsg;
    //    1.获取宽度，获取字符串不折行单行显示时所需要的长度
    CGSize titleSize = [tips.text sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(MAXFLOAT, 30)];
    tips.frame = CGRectMake(self.viewContaint.frame.size.width/2-titleSize.width/2, 20, titleSize.width, titleSize.height+2);
    tips.textColor = [UIColor blackColor];
    [self.viewContaint addSubview:tips];
}

- (void)addQuitButton{
    UIImageView *quitImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"退出程序"]];
    quitImageView.center = CGPointMake(self.viewContaint.frame.size.width/2, 160);
    [self.viewContaint addSubview:quitImageView];
    quitImageView.userInteractionEnabled = YES;
    UIGestureRecognizer *tap= [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(quitClick)];
    [quitImageView addGestureRecognizer:tap];
}

- (void)quitClick{
    if([_delegate respondsToSelector:@selector(itemClick:type:)]){
        [_delegate itemClick:self type:ClickTypeQuit];
    }
}

- (void)addChangeButton{
    UIImageView *changeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_change_login"]];
    changeImageView.center = CGPointMake(self.viewContaint.frame.size.width/2, 100);
    [self.viewContaint addSubview:changeImageView];
    changeImageView.userInteractionEnabled = YES;
    UIGestureRecognizer *tap= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeLoginClick)];
    [changeImageView addGestureRecognizer:tap];
}

- (void)changeLoginClick{
    if([_delegate respondsToSelector:@selector(itemClick:type:)]){
        [_delegate itemClick:self type:ClickTypeChange];
    }
}

- (void)closeDialog{
    [self removeFromSuperview];
}


@end
